<?php defined('SYSPATH') or die('No direct script access.');
/**
* Function Helper
*/
class Helper_myFunctions {
	
	static function &factory() {
	   $o = new Helper_myFunctions();
	  return $o;	
	}
	
  // Show Pagination
	function paginations($count, $limit)
	{
		$pages = Pagination::factory(array(
			'current_page'   => array('source' => 'query_string', 'key' => 'page'),
			'total_items'    => $count,
			'items_per_page' => $limit,
			'auto_hide'      => TRUE,
			'view'           => 'pagination/csz',
		));
		
		return $pages->render();
	}
	
  // Create Leading Zero of Number
	public function leading_zero($number_int,$length)
	{
		return sprintf('%0'.$length.'u', $number_int);
	}

  // Put String after the FileName and Before the File Extention
	public function set_filename($filename,$show='thumb')
	{
		$the_value = str_ireplace('.','_'.$show.'.',$filename);
		
		return $the_value;
	}
	
  // Formating Text from Text Area to Echo with Line Break
	public function format_text($text)
	{
		$the_value = nl2br($text);
		
		return $the_value;
	}

	public function int_to_text($angka)
	{
		$ambil = array("", "Satu", "Dua", "Tiga", "Empat", "Lima", "Enam", "Tujuh", "Delapan", "Sembilan", "Sepuluh", "Sebelas");
		if ($angka < 12)
		return " " . $ambil[$angka];
		elseif ($angka < 20)
		return $this->int_to_text($angka - 10) . "Belas";
		elseif ($angka < 100)
		return $this->int_to_text($angka / 10) . " Puluh" . $this->int_to_text($angka % 10);
		elseif ($angka < 200)
		return " Seratus" . $this->int_to_text($angka - 100);
		elseif ($angka < 1000)
		return $this->int_to_text($angka / 100) . " Ratus" . $this->int_to_text($angka % 100);
		elseif ($angka < 2000)
		return " Seribu" . $this->int_to_text($angka - 1000);
		elseif ($angka < 1000000)
		return $this->int_to_text($angka / 1000) . " Ribu" . $this->int_to_text($angka % 1000);
		elseif ($angka < 1000000000)
		return $this->int_to_text($angka / 1000000) . " Juta" . $this->int_to_text($angka % 1000000);
		
	}
	
  // Remove duplicate array from two array and merged it to one array
	public function check_duplicate_array($p1, $p2)
	{
 		$r = array();
		 
		foreach($p2 as $key=>$id)
		{
			  if (!in_array($id,$p1))
			  {
			  	 $r[] = $id;
			  }
		}
		return $r;
	}
	
	/* Count record data from $table
	* $where format : array(array('where1','value1'),array('where2','value2'))
	*/
	public function count_total_data($table,$where=NULL,$limit=NULL,$offset=NULL)
	{
		$find_value = DB::select()->from($table);
		
		if(!is_null($offset))
		{
			$find_value = $find_value->offset($offset);
		}
		
		if(!is_null($limit))
		{
			$find_value = $find_value->limit($limit);
		}
		
		if(!is_null($where))
		{
			foreach($where as $find_data)
			{
				switch ($find_data[2])
				{
				case "LIKE":
						  $find_value = $find_value->where($find_data[0],'LIKE','%'.$find_data[1].'%');
						  break;
				
				case "IN":
						  $find_value = $find_value->where($find_data[0],'IN',$find_data[1]);
						  break;
				
				default:
						  $find_value = $find_value->where($find_data[0],'=',$find_data[1]);
						  break;
				}
			}
		}
		
		$find_value = $find_value->execute();
		
		$total_data = $find_value->count();
		
		return $total_data;
	}
	
	/* Create Simple Data Lists
	* $where format : array(array('where1','value1'),array('where2','value2'))
	*/
	public function create_data_lists($table,$where=NULL,$orders=NULL,$limit=NULL,$offset=NULL)
	{
		$find_value = DB::select()->from($table);
		
		if(!is_null($offset))
		{
			$find_value = $find_value->offset($offset);
		}
		
		if(!is_null($limit))
		{
			$find_value = $find_value->limit($limit);
		}
		
		if(!is_null($where))
		{
			foreach($where as $find_data)
			{
				switch ($find_data[2])
				{
				case "LIKE":
						  $find_value = $find_value->where($find_data[0],'LIKE','%'.$find_data[1].'%');
						  break;
				
				case "IN":
						  $find_value = $find_value->where($find_data[0],'IN',$find_data[1]);
						  break;
				
				default:
						  $find_value = $find_value->where($find_data[0],'=',$find_data[1]);
						  break;
				}
			}
		}
		
		if(!is_null($orders))
		{
			foreach($orders as $order)
			{
				$find_value = $find_value->order_by($order[0],$order[1]);
			}
		}
		
		$find_value = $find_value->execute();
		
		$show_data = $find_value->as_array();
		
		return $show_data;
	}
	
	public function check_child($id,$table,$field)
	{
		$query = DB::select()
			->from($table)
			->where($field,'=',$id)
			->execute();
		$count = $query->count();
		
		if($count >= 1) {
			return TRUE;
		} else {
			return FALSE;
		}
	}
	
	
	
	
	
	
	
	
	
	
	
	
}